{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n",
      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
      "Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n",
      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
      "Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n",
      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n",
      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n",
      "0 0.6903\n",
      "1 0.8209\n",
      "2 0.8602\n",
      "3 0.8789\n",
      "4 0.8884\n",
      "5 0.8946\n",
      "6 0.8979\n",
      "7 0.9008\n",
      "8 0.9036\n",
      "9 0.9051\n",
      "10 0.9081\n",
      "11 0.9099\n",
      "12 0.9113\n",
      "13 0.9125\n",
      "14 0.9143\n",
      "15 0.9146\n",
      "16 0.9154\n",
      "17 0.9163\n",
      "18 0.9174\n",
      "19 0.9176\n",
      "20 0.918\n",
      "21 0.9187\n",
      "22 0.9192\n",
      "23 0.92\n",
      "24 0.9202\n",
      "25 0.9202\n",
      "26 0.9207\n",
      "27 0.9213\n",
      "28 0.9219\n",
      "29 0.9224\n",
      "30 0.9228\n",
      "31 0.9233\n",
      "32 0.9244\n",
      "33 0.9248\n",
      "34 0.9252\n",
      "35 0.9259\n",
      "36 0.9264\n",
      "37 0.9269\n",
      "38 0.9268\n",
      "39 0.927\n",
      "40 0.9276\n",
      "41 0.9281\n",
      "42 0.9286\n",
      "43 0.9292\n",
      "44 0.9297\n",
      "45 0.9302\n",
      "46 0.9309\n",
      "47 0.9312\n",
      "48 0.9319\n",
      "49 0.9328\n",
      "50 0.9339\n",
      "51 0.9346\n",
      "52 0.9356\n",
      "53 0.9363\n",
      "54 0.937\n",
      "55 0.9373\n",
      "56 0.9379\n",
      "57 0.9383\n",
      "58 0.9387\n",
      "59 0.9392\n",
      "60 0.9393\n",
      "61 0.9398\n",
      "62 0.9403\n",
      "63 0.9409\n",
      "64 0.9415\n",
      "65 0.942\n",
      "66 0.9427\n",
      "67 0.943\n",
      "68 0.9435\n",
      "69 0.944\n",
      "70 0.9445\n",
      "71 0.9446\n",
      "72 0.945\n",
      "73 0.9453\n",
      "74 0.9456\n",
      "75 0.9463\n",
      "76 0.9467\n",
      "77 0.9468\n",
      "78 0.9475\n",
      "79 0.9478\n",
      "80 0.9483\n",
      "81 0.9487\n",
      "82 0.9488\n",
      "83 0.9491\n",
      "84 0.9493\n",
      "85 0.95\n",
      "86 0.9506\n",
      "87 0.951\n",
      "88 0.9516\n",
      "89 0.9518\n",
      "90 0.9521\n",
      "91 0.9528\n",
      "92 0.9531\n",
      "93 0.9536\n",
      "94 0.9539\n",
      "95 0.9541\n",
      "96 0.9545\n",
      "97 0.9547\n",
      "98 0.9552\n",
      "99 0.9554\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "\n",
    "def init_weights(shape):\n",
    "    return tf.Variable(tf.random_normal(shape, stddev=0.01))\n",
    "\n",
    "\n",
    "def model(X, w_h, w_o):\n",
    "    h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions\n",
    "    return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us\n",
    "\n",
    "\n",
    "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n",
    "trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels\n",
    "\n",
    "X = tf.placeholder(\"float\", [None, 784])\n",
    "Y = tf.placeholder(\"float\", [None, 10])\n",
    "\n",
    "w_h = init_weights([784, 625]) # create symbolic variables\n",
    "w_o = init_weights([625, 10])\n",
    "\n",
    "py_x = model(X, w_h, w_o)\n",
    "\n",
    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # compute costs\n",
    "train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct an optimizer\n",
    "predict_op = tf.argmax(py_x, 1)\n",
    "\n",
    "# Launch the graph in a session\n",
    "with tf.Session() as sess:\n",
    "    # you need to initialize all variables\n",
    "    tf.global_variables_initializer().run()\n",
    "\n",
    "    for i in range(100):\n",
    "        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):\n",
    "            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})\n",
    "        print(i, np.mean(np.argmax(teY, axis=1) ==\n",
    "                         sess.run(predict_op, feed_dict={X: teX})))\n",
    "\n",
    "#source: https://github.com/tflearn/tflearn/tree/master/examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
